name: nv-mii

on:
  workflow_dispatch:
    inputs:
      mii_branch:
        description: 'DeepSpeed-MII Branch'
        required: false
        default: 'main'
        type: string
  pull_request:
    paths:
      - '.github/workflows/nv-mii.yml'
      - 'requirements/**'
      - 'setup.py'
      - 'deepspeed/__init__.py'
      - 'deepspeed/inference/**'
      - '!deepspeed/inference/v2/**' # exclude v2 dir
  merge_group:
    branches: [ master ]
  schedule:
    - cron: "0 0 * * *"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  unit-tests:
    runs-on: [self-hosted, nvidia, cu116, v100]

    steps:
      - uses: actions/checkout@v3

      - id: setup-venv
        uses: ./.github/workflows/setup-venv

      - name: Install pytorch
        run: |
          pip3 install -U --cache-dir $TORCH_CACHE torch --index-url https://download.pytorch.org/whl/cu118
          python -c "import torch; print('torch:', torch.__version__, torch)"
          python -c "import torch; print('CUDA available:', torch.cuda.is_available())"

      - name: Install transformers
        run: |
          git clone https://github.com/huggingface/transformers
          cd transformers
          # if needed switch to the last known good SHA until transformers@master is fixed
          # git checkout 1cc453d33
          git rev-parse --short HEAD
          pip install .

      - name: Install deepspeed
        run: |
          pip install .[dev]
          ds_report

      - name: Python environment
        run: |
          pip list

      - name: MII unit tests
        run: |
          BRANCH="main"
          if [[ ! -z "${{ github.event.inputs.mii_branch }}" ]]; then
              BRANCH="${{ github.event.inputs.mii_branch }}"
          fi
          echo "Cloning DeepSpeed-MII branch: $BRANCH"
          git clone -b $BRANCH --depth=1 https://github.com/microsoft/DeepSpeed-MII.git
          cd DeepSpeed-MII
          pip install .[dev]
          unset TORCH_CUDA_ARCH_LIST # only jit compile for current arch
          cd tests/legacy
          pytest $PYTEST_OPTS --forked -m "deepspeed" ./
